package com.zshy.base.core.contract.exception;

import com.zshy.base.core.contract.enums.IEnum;
import com.zshy.base.core.contract.enums.ResultCodeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * 服务异常，可用于控制业务异常流程，抛出后由统一异常增强类捕获，返回友好提示
 *
 * @author zhouhengzhe
 * @公众号 小白的Java进阶之路
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ServiceException extends RuntimeException implements Serializable {

    private final Integer code;

    private final String message;

    public ServiceException() {
        this(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL.getDesc());
    }

    public ServiceException(String message) {
        this(ResultCodeEnum.FAIL.getCode(), message);
    }

    public ServiceException(Integer code, String message) {
        this.message = message;
        this.code = code;
    }

    public ServiceException(Throwable e) {
        this(e.getMessage());
    }

    public ServiceException(IEnum<Integer> resultCode) {
        this(resultCode.getCode(), resultCode.getDesc());
    }

}
